package com.frxuexi.myroom.view

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.CheckCircle
import androidx.compose.material3.Button
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.Divider
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.navigation.NavHostController
import coil3.compose.AsyncImage
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.BaseTitle
import com.frxuexi.lib_base.UiState
import com.frxuexi.lib_base.room.Leave
import com.frxuexi.myroom.intent.LeaveIntent
import com.frxuexi.myroom.viewmodel.LeaveViewModel
import com.google.gson.Gson
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.launch

@Composable
fun ApproveLeaveView(
    navHostController: NavHostController,
    json: String,
    vm: LeaveViewModel = hiltViewModel()
) {
    val entity = Gson().fromJson(json, Leave::class.java)
    val kv = MMKV.defaultMMKV()

//    var uname = kv.decodeString("uname", "未登录")!!
    var comReason by remember {
        mutableStateOf("")
    }
    var isOk by remember {
        mutableStateOf("通过")
    }

    LaunchedEffect(key1 = "", block = {
        launch {
            vm.state.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        ToastUtils.showLong("审批完成")
                        navHostController.popBackStack()
                    }

                    else -> {}
                }
            }
        }
    })
    Column(
        Modifier
            .fillMaxSize()
            .padding(20.dp), verticalArrangement = Arrangement.spacedBy(10.dp)
    ) {
        BaseTitle(navHostController = navHostController, title = "请假详情")

        Text(text = "申报人信息", color = Color.Blue)
        ElevatedCard(colors = CardDefaults.cardColors(containerColor = Color.White)) {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(20.dp, 10.dp)
            ) {
                AsyncImage(
                    model = "https://img1.baidu.com/it/u=4035546522,389745751&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1735146000&t=a6cb452f25f5456854d0cc65889449b6",
                    contentDescription = "",
                    modifier = Modifier
                        .size(80.dp)
                        .clip(CircleShape),
                    contentScale = ContentScale.Crop
                )
                Spacer(modifier = Modifier.width(10.dp))
                Column (Modifier.height(80.dp), verticalArrangement = Arrangement.SpaceAround){
                    Text(text = entity.uname)
                    Text(text = entity.role)
                }
            }
        }

        Text(text = "请假详情", color = Color.Blue)
        Text(text = "请假类型：${entity.type}")
        Text(text = "开始时间：${entity.startTime}")
        Text(text = "结束时间：${entity.endTime}")
        Text(text = "请假原因：${entity.reason}")

        Spacer(modifier = Modifier.height(10.dp))

        Text(text = "申报意见", color = Color.Blue)
        Divider()
        Row(
            horizontalArrangement = Arrangement.spacedBy(8.dp),
            verticalAlignment = Alignment.CenterVertically
        ) {
            Text(text = "意见：")
            RadioButton(selected = isOk == "通过", onClick = {
                isOk = "通过"
            })
            Text(text = "通过")
            RadioButton(selected = isOk == "拒绝", onClick = {
                isOk = "拒绝"
            })
            Text(text = "拒绝")
        }
        Row(horizontalArrangement = Arrangement.spacedBy(8.dp)) {
            Text(text = "内容：")
            OutlinedTextField(
                modifier = Modifier.fillMaxWidth(),
                value = comReason,
                onValueChange = {
                    comReason = it
                })

        }

        Button(modifier = Modifier.fillMaxWidth(), onClick = {
            entity.comReason=comReason
            entity.status=isOk
            entity.comTime=vm.toDateTime(System.currentTimeMillis())
            vm.sendIntent(LeaveIntent.approveLeave(entity))
            /*vm.sendIntent(
                UserIntent.approveLeave(
                    mapOf(
                        "id" to entity.id,
                        "status" to isOk,
                        "comTime" to vm.toDateTime(System.currentTimeMillis()),
                        "comReason" to comReason
                    )
                )
            )*/
        }) {
            Text(text = "提交")
        }
    }
}